﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AGPR.Locadora.Model;
using AGPR.Locadora.Utils;
using AGPR.Locadora.Web.Models;

namespace AGPR.Locadora.Web.Controllers
{
    /// <summary>
    /// Controlador do Cadastro de Clientes
    /// </summary>
    public class ClientesController : BaseController
    {
        private static Clientes clientes = new Clientes();

        #region Index
        public ActionResult Index()
        {
            return RedirectToAction("consultar");
        }
        #endregion

        #region Cadastrar Cliente
        public ActionResult Cadastrar()
        {
            ViewBag.Title = "Cadastrar Cliente";

            return View();
        }

        [HttpPost]
        public ActionResult Cadastrar(FormCollection cliente)
        {
            clientes.Cadastrar(cliente);

            return this.Index();
        }
        #endregion

        #region Alterar Cliente

        public ActionResult Alterar(int id)
        {
            var item = Cliente.Get(id);

            if (item == null)
            {
                return this.Index();
            }

            ViewBag.Title = "Alterar Cliente";
            ViewBag.Cliente = item;

            return View();
        }

        [HttpPost]
        public ActionResult Alterar(int id, FormCollection cliente)
        {
            ViewBag.Title = "Alterar Cliente";
            ViewBag.Cliente = clientes.Alterar(cliente);

            return this.Index();
        }

        #endregion

        #region Excluir Cliente

        public ActionResult Excluir(int id)
        {
            var item = Cliente.Get(id);

            if (item != null)
            {
                item.Excluir();
            }

            return Redirect(Request.UrlReferrer.PathAndQuery);
        }

        #endregion

        #region Visualizar Cliente

        public ActionResult Visualizar(int id)
        {
            var item = Cliente.Get(id);

            if (item == null)
            {
                return this.Index();
            }

            ViewBag.Title = "Visualizar";
            ViewBag.Cliente = item;

            if (Request.IsAjaxRequest())
            {
                return PartialView();
            }
            else
            {
                return View();
            }
        }

        #endregion

        #region Consultar Clientes

        public ActionResult Consultar(int p = 1)
        {
            ViewBag.Title = "Consulta de Clientes";
            ViewBag.Total = Cliente.Total();
            ViewBag.Paginacao = Paginacao.init(ViewBag.Total, p);            
            ViewBag.Clientes = Cliente.GetAll(ViewBag.Paginacao.Offset);
            ViewBag.TotalPagina = ViewBag.Clientes.Count;

            return View();
        }

        #endregion
    }
}
